home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MSTPChart / WindowControl.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-26  |  6.6 KB  |  199 lines  |  [TEXT/KAHL]

  1. #include    "MST_Defines.h"
  2. #include    "MST_Externs.h"
  3. #include    "MST_Prototypes.h"
  4.  
  5.  
  6. /*_____________________________________________________________________________
  7.                         Init_Windows()- set all the window pointers to NIL
  8. _____________________________________________________________________________*/
  9.  
  10.  
  11. void    Init_Windows    (void)
  12. {
  13.     AboutMSTDlog        =    NIL;
  14.     PeriodicChartDlog    =    NIL;
  15.     ElementsDlog        =    NIL;
  16. }
  17.  
  18.  
  19. /*_____________________________________________________________________________
  20.                         Activate_Window()- handle activate events
  21. _____________________________________________________________________________*/
  22.  
  23.  
  24.  
  25. void    Activate_Window    (WindowPtr thisWindow)
  26. {
  27.     GDHandle    saveDevice;
  28.     CGrafPtr    saveCGraphPtr;
  29.     
  30.     GetGWorld    (&saveCGraphPtr,&saveDevice);    /*We just need to know the device*/
  31.                                                 /*so we can pass the info to*/
  32.                                                 /*SetGWorld later*/
  33. /*
  34. •        We determine which window we are working with by examining the window's
  35. •        reference constant. The reference constant (called the RefCon) is a
  36. •        four byte field in the window's data structure which is available for
  37. •        the programmer to use. Often programmers store a handle to a custom
  38. •        structure which stores data associated with the window (such as editable
  39. •        text and style information for a word processor). In this program, we have
  40. •        stored the resource ID number of the dialog template in the RefCon field of the
  41. •        dialog template, and use it to distinguish what kind of window (About,
  42. •        Periodic, or Element) we are working with. Other methods abound. In some
  43. •        cases it might make sense to use the window's title. In others, you
  44. •        might store a unique identifier along with other data in a structure
  45. •        you access via a handle you store in the window's RefCon. Keep in mind
  46. •        also the following. Most operations on windows of the same "category" will
  47. •        work the same, which will greatly simplify your program. For example, if
  48. •        we consider "document" windows of a word processor as a category, only a
  49. •        single update routine, a single save routine, etc are required for the
  50. •        category, regardless of the number of open windows.
  51. */
  52.     switch (GetWRefCon(thisWindow))
  53.     {
  54.         case    ResID_AboutMST:
  55.         case    ResID_PeriodicChart:
  56.         case    ResID_Elements:
  57.         {
  58. /*
  59. •        Make Quickdraw's current port your window you are activating.
  60. */
  61.             SetGWorld    ((CGrafPtr)thisWindow,    saveDevice);
  62. /*
  63. •        Here you would handle any changes called for when the active window is
  64. •        changed. For example, your menus might be different for one window than
  65. •        another, or you might need to highlight/unhighlight a scrollbar.
  66. */
  67.             break;
  68.         }
  69.         default:            // ••• Consider an unknown window as a fatal error.
  70.         {
  71.             SysBeep(1);
  72.             ExitToShell    ();
  73.             break;
  74.         }
  75.     }
  76. }    
  77.  
  78.  
  79. /*_____________________________________________________________________________
  80.                         Do_TEIdle()- not used
  81. _____________________________________________________________________________*/
  82.  
  83.  
  84.  
  85.  
  86. void    Do_TEIdle    (TEHandle theInput)
  87. {
  88.     // ••• Not necessary for this program. Handles some routine TextEdit housekeeping.
  89. }
  90.  
  91.  
  92. /*_____________________________________________________________________________
  93.                         Close_Window()- choose the appropriate window closing routine
  94. _____________________________________________________________________________*/
  95.  
  96.  
  97.  
  98. void    Close_Window    (WindowPtr    thisWindow)
  99. /*
  100.  •        Note that the system generates a pair of activate/deactivate events
  101.  •        whenever a new window comes to the fore. Closing a window, however,
  102.  •        will only generate an activate event, since there is no window to
  103.  •        deactivate. More complex programs than this one often do significant
  104.  •        housekeeping tasks in their deactivate routines. If your program does this,
  105.  •        you may need to do the following. First, hide the window you intend to close,
  106.  •        bringing your next window to the fore. This generates a pair of
  107.  •        activate/deactivate events. Call WaitNextEvent twice with the mask
  108.  •        activMask so you only retrieve activate/deactivate events, and process
  109.  •        them immediately. Now you can dispose of the window you are closing.
  110.  •
  111.  •        See the note about reference constants (RefCon's) in the function
  112.  •        Activate_Window() above to understand the use of GetWRefCon() below.
  113. */
  114. {
  115.     switch (GetWRefCon(thisWindow)) 
  116.     {   
  117.         case    ResID_AboutMST:            {    Close_AboutMST();        break;    }
  118.         case    ResID_PeriodicChart:    {    Close_PeriodicChart();    break;    }
  119.         case    ResID_Elements:            {    Close_Elements();        break;    }
  120.     }
  121. }
  122.  
  123.  
  124. /*_____________________________________________________________________________
  125.                         Do_Window()- choose the appropriate routine to handle
  126.                                      an event in the window's content region
  127. _____________________________________________________________________________*/
  128.  
  129.  
  130.  
  131. void    Do_Window    (WindowPtr thisWindow,EventRecord *thisEvent)
  132. {
  133.     GDHandle    saveDevice;
  134.     CGrafPtr    saveCGrafPtr;
  135.  
  136.     GetGWorld    (&saveCGrafPtr,&saveDevice);
  137.     SetGWorld    ((CGrafPtr)thisWindow,saveDevice);
  138. /*
  139.  •        See the note about reference constants (RefCon's) in the function
  140.  •        Activate_Window() above to understand the use of GetWRefCon() below.
  141. */
  142.  
  143.     switch    (GetWRefCon(thisWindow))
  144.     {
  145.         case    ResID_AboutMST:            {    Do_AboutMST            (thisEvent);    break;    }
  146.         case    ResID_PeriodicChart:    {    Do_PeriodicChart    (thisEvent);    break;    }
  147.         case    ResID_Elements:            {    Do_Elements            (thisEvent);    break;    }
  148.     }
  149.     SetGWorld        (saveCGrafPtr,saveDevice);
  150. }
  151.  
  152. /*_____________________________________________________________________________
  153.                         Do_Key()- handle a key event
  154. _____________________________________________________________________________*/
  155.  
  156.  
  157. void    Do_Key    (WindowPtr thisWindow,EventRecord *thisEvent)
  158. {    
  159.     long            testKey,keyCode,loop1;
  160.  
  161.     if    ((thisEvent->modifiers & cmdKey) == 0)
  162.     {
  163.         testKey         = BitAnd    (thisEvent->message,charCodeMask);    /*leftover line from another program*/
  164.         keyCode         = BitAnd    (thisEvent->message,keyCodeMask);
  165.         keyCode        /= 256;
  166.         if    (keyCode==53)
  167.         {
  168.             switch    (GetWRefCon(thisWindow))
  169.             {
  170.                 case    ResID_AboutMST:            {    Close_AboutMST            ();    break;    }
  171.                 case    ResID_PeriodicChart:    {    Close_PeriodicChart        ();    break;    }
  172.                 case    ResID_Elements:            {    Close_Elements            ();    break;    }
  173.             }
  174.         }
  175.     }
  176. }    
  177.  
  178. /*_____________________________________________________________________________
  179.                         Update_Window()- choose the appropriate update routine
  180. _____________________________________________________________________________*/
  181.  
  182.  
  183. void    Update_Window    (EventRecord *thisEvent)
  184. {
  185.     WindowPtr    thisWindow;
  186.     
  187.     thisWindow = (WindowPtr)thisEvent->message;
  188. /*
  189.  •        See the note about reference constants (RefCon's) in the function
  190.  •        Activate_Window() above to understand the use of GetWRefCon() below.
  191. */
  192.     switch (GetWRefCon(thisWindow))
  193.     {
  194.         case    ResID_AboutMST:            {    UpDate_AboutMST();        break;    }
  195.         case    ResID_PeriodicChart:    {    UpDate_PeriodicChart();    break;    }
  196.         case    ResID_Elements:            {    UpDate_Elements();        break;    }
  197.     }
  198. }    
  199.